<template>
  <div class="app-container">


    <div class="page-content" style="display: flex;justify-content: center">
      <div style="position: absolute;left: 50px"><i class="el-icon-s-unfold"></i></div>
      <span>活动管理</span>
    </div>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="activityList">
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="活动名称" align="center" prop="name"/>
      <el-table-column label="封面" align="center" prop="pic" width="180">
        <template slot-scope="scope">
          <div v-if="!scope.row.pic">暂无图像</div>
          <div v-else>
            <el-image
                style="width: 50px; height: 50px"
                :src="scope.row.pic"
                :preview-src-list="[scope.row.pic]">
            </el-image>
          </div>

        </template>
      </el-table-column>
<!--      <el-table-column label="时间" align="center" prop="time" width="180">
      </el-table-column>-->
      <el-table-column label="背景" align="center" prop="background"/>
      <el-table-column label="文化内涵" align="center" prop="connotation"/>
      <el-table-column label="传承人" align="center" prop="inheritor"/>
      <el-table-column label="可预约人数" align="center" prop="num"/>
      <el-table-column label="演出地点" align="center" prop="address"/>
      <el-table-column label="结束预约时间" align="center" prop="endTime" width="180">
      </el-table-column>
      <el-table-column label="开始预约时间" align="center" prop="startTime" width="180">
      </el-table-column>
      <el-table-column label="详细信息" align="center" prop="content" width="180" show-overflow-tooltip/>
      <el-table-column label="价格" align="center" prop="price"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :page-sizes="[10,20,50]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
    />

    <!-- 添加或修改活动信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称"/>
        </el-form-item>
        <el-form-item label="封面">
          <imageUpload v-model="form.pic"/>
        </el-form-item>
        <el-form-item label="背景" prop="background">
          <el-input v-model="form.background" placeholder="请输入背景"/>
        </el-form-item>
        <el-form-item label="文化内涵" prop="connotation">
          <el-input v-model="form.connotation" placeholder="请输入文化内涵"/>
        </el-form-item>

        <el-form-item label="传承人" prop="inheritor">
          <el-input v-model="form.inheritor" placeholder="请输入传承人"/>
        </el-form-item>


        <el-form-item label="演出地点" prop="address">
          <el-select v-model="form.address" placeholder="请选择地点">

            <div v-for="item in museumList">
              <el-option :label="item.name" :value="item.name"/>
            </div>

          </el-select>
        </el-form-item>



        <el-form-item label="可预约人数" prop="num">
          <el-input v-model="form.num" placeholder="请输入可预约人数"/>
        </el-form-item>
        <el-form-item label="结束预约时间" prop="endTime">
          <el-date-picker clearable size="small"
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择结束预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始预约时间" prop="startTime">
          <el-date-picker clearable size="small"
                          v-model="form.startTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择开始预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="详细信息" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listActivity, getActivity, delActivity, addActivity, updateActivity} from "@/api/project/activity";
import {listMuseum} from "@/api/project/museum";

export default {
  name: "Activity",
  data() {
    return {
      loading: true,
      // 总条数
      total: 0,
      // 活动信息表格数据
      activityList: [],
      museumList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        pic: null,
        time: null,
        background: null,
        connotation: null,
        inheritor: null,
        coordinate: null,
        num: null,
        endTime: null,
        startTime: null,
        content: null,
        collection: null,
        type: null,
        price: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handlePageChange(currentPage) {
      this.queryParams.pageNum = currentPage
      this.getList()
    },
    handleSizeChange(pageSize) {
      this.queryParams.pageSize = pageSize
      this.getList()
    },
    /** 查询活动信息列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        let list, start, end
        start = (this.queryParams.pageNum - 1) * this.queryParams.pageSize
        end = start + this.queryParams.pageSize
        list = response.data.filter((item, index) => {
          return index >= start && index < end
        })
        this.activityList = list;


        listMuseum().then(res=>{
          this.museumList=res.data
        })
        this.total = response.data.length;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        pic: null,
        time: null,
        background: null,
        connotation: null,
        inheritor: null,
        coordinate: null,
        num: null,
        endTime: null,
        startTime: null,
        content: null,
        collection: null,
        type: null,
        price: null
      };
      // this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getActivity(row).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {

      this.form.type='普通类型'
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除？').then(function () {
        return delActivity(row);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
